package com.zzyl.nursing.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.dto.task.NursingTaskCancelDto;
import com.zzyl.nursing.dto.task.NursingTaskDoDto;
import com.zzyl.nursing.dto.task.NursingTaskListDto;
import com.zzyl.nursing.dto.task.NursingTaskUpdateTimeDto;
import com.zzyl.nursing.mapper.NursingElderMapper;
import com.zzyl.nursing.service.ICheckInService;
import com.zzyl.nursing.service.IElderService;
import com.zzyl.nursing.vo.task.NursingTaskInfoVo;
import com.zzyl.nursing.vo.task.NursingTaskVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingTaskMapper;
import com.zzyl.nursing.domain.NursingTask;
import com.zzyl.nursing.service.INursingTaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 护理任务Service业务层处理
 * 
 * @author alexis
 * @date 2024-11-17
 */
@Service
public class NursingTaskServiceImpl extends ServiceImpl<NursingTaskMapper, NursingTask> implements INursingTaskService
{
    @Autowired
    private NursingTaskMapper nursingTaskMapper;
    @Autowired
    private IElderService elderService;
    @Autowired
    private NursingElderMapper nursingElderMapper;
    @Autowired
    private ICheckInService checkInService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询护理任务
     * 
     * @param id 护理任务主键
     * @return 护理任务
     */
    @Override
    public NursingTask selectNursingTaskById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询护理任务列表
     *
     * @return 护理任务
     */
    @Override
    public List<NursingTaskVo> selectNursingTaskList(NursingTaskListDto nursingTaskListDto)
    {
        List<NursingTask> nursingTasks = nursingTaskMapper.selectNursingTaskList(nursingTaskListDto);
        List<NursingTaskVo> nursingTaskVos =  new ArrayList<>();
        nursingTasks.forEach(nursingTask -> {
            NursingTaskVo nursingTaskVo = BeanUtil.toBean(nursingTask, NursingTaskVo.class);
            Elder elder = elderService.selectElderById(nursingTask.getElderId());
            nursingTaskVo.setAge(DateUtil.ageOfNow(elder.getBirthday()));
            // 获取 nursingId 并拆分
            String nursingId = nursingTaskVo.getNursingId();
            if (nursingId != null && !nursingId.isEmpty()) {
                String[] nursingIds = nursingId.split(",");
                // 根据 nursingIds 查询护理员名字列表
                List<String> nursingNames = nursingElderMapper.selectNursingNamesByIds(Arrays.asList(nursingIds));
                // 设置到 VO 中
                nursingTaskVo.setNursingName(nursingNames);
            }
            nursingTaskVos.add(nursingTaskVo);
        });
        return nursingTaskVos;
    }

    /**
     * 新增护理任务
     * 
     * @param nursingTask 护理任务
     * @return 结果
     */
    @Override
    public int insertNursingTask(NursingTask nursingTask)
    {
        return save(nursingTask) ? 1 : 0;
    }

    /**
     * 修改护理任务
     * 
     * @param nursingTask 护理任务
     * @return 结果
     */
    @Override
    public int updateNursingTask(NursingTask nursingTask)
    {
        return updateById(nursingTask) ? 1 : 0;
    }

    /**
     * 批量删除护理任务
     * 
     * @param ids 需要删除的护理任务主键
     * @return 结果
     */
    @Override
    public int deleteNursingTaskByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理任务信息
     * 
     * @param id 护理任务主键
     * @return 结果
     */
    @Override
    public int deleteNursingTaskById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public NursingTaskInfoVo getTaskById(Long id) {
        NursingTask nursingTask = nursingTaskMapper.selectNursingTaskById(id);
        NursingTaskInfoVo nursingTaskInfoVo = BeanUtil.toBean(nursingTask, NursingTaskInfoVo.class);
        Elder elder = elderService.selectElderById(nursingTask.getElderId());
        nursingTaskInfoVo.setAge(DateUtil.ageOfNow(elder.getBirthday()));
        //有bug
        String updater = (String)redisTemplate.opsForValue().get("updater" + id);
        nursingTaskInfoVo.setUpdater(updater);

        nursingTaskInfoVo.setSex(elder.getSex()==0?"女":"男");
        Long elderId = nursingTaskInfoVo.getElderId();
        String nursingLevelName = checkInService.selectCheckInNursingLevelName(elderId);
        nursingTaskInfoVo.setNursingLevelName(nursingLevelName);
        List<String> nursingNames = nursingElderMapper.selectNursingNamesByIds(Arrays.asList(nursingTask.getNursingId().split(",")));
        nursingTaskInfoVo.setNursingName(nursingNames);
        return nursingTaskInfoVo;
    }

    @Override
    public void canselNursingTaskByIds(NursingTaskCancelDto nursingTaskCancelDto) {
        // 构建更新对象
        NursingTask nursingTask = new NursingTask();
        nursingTask.setId(nursingTaskCancelDto.getTaskId());
        nursingTask.setStatus(3); // 设置状态为已取消
        nursingTask.setCancelReason(nursingTaskCancelDto.getReason());
        Long updateId = UserThreadLocal.getUserId();
        if (ObjectUtil.isNotEmpty(updateId)) {
            String name = nursingElderMapper.selectUserNameById(updateId);
            redisTemplate.opsForValue().set("updater" + nursingTaskCancelDto.getTaskId(), name);
        }
        updateById(nursingTask);
    }


    @Override
    public void doNursingTaskByIds(NursingTaskDoDto nursingTaskDoDto) {
        NursingTask nursingTask = new NursingTask();
        nursingTask.setId(nursingTaskDoDto.getTaskId());
        nursingTask.setMark(nursingTaskDoDto.getMark());
        nursingTask.setEstimatedServerTime(nursingTaskDoDto.getEstimatedServerTime());
        nursingTask.setStatus(2);
        nursingTask.setTaskImage( nursingTaskDoDto.getTaskImage());
        Long updateId = SecurityUtils.getUserId();
        if (ObjectUtil.isNotEmpty(updateId)) {
            String name = nursingElderMapper.selectUserNameById(updateId);
            redisTemplate.opsForValue().set("updater" + nursingTaskDoDto.getTaskId(), name);
        }
        updateById(nursingTask);
    }

    @Override
    public void updateNursingTaskTimeByIds(NursingTaskUpdateTimeDto nursingTaskUpdateTimeDto) {
        NursingTask nursingTask = new NursingTask();
        nursingTask.setId(nursingTaskUpdateTimeDto.getTaskId());
        nursingTask.setEstimatedServerTime(nursingTaskUpdateTimeDto.getEstimatedServerTime());
        Long updateId = SecurityUtils.getUserId();
        if (ObjectUtil.isNotEmpty(updateId)) {
            String name = nursingElderMapper.selectUserNameById(updateId);
            redisTemplate.opsForValue().set("updater" + nursingTaskUpdateTimeDto.getTaskId(), name);
        }
        updateById(nursingTask);
    }
}
